﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Model;
using BLL;
using System.Data.SqlClient;
using System.Runtime.Serialization.Json;
using Newtonsoft.Json;
using Common;


namespace DiChanAPI.Controllers
{
    public class IndexController : ApiController
    {
        #region  ///菜单功能
        // GET api/index
        [HttpGet]
        [ActionName("GetMenu")]
        public string  GetMenu(int id)
        {
            SqlParameter[] parmeter={
                                        new SqlParameter("@Roles_Id",id),
                                    };
            var list = new AuthorityAndRolesBLL().GetList(parmeter);

            List<Authority> getList = null;
            var query = new AuthorityBLL().GetListAuthority().Where(n => n.ParentId == 0);
            foreach(var item in query)
            {
                if(list.Count(p=>p.Authority_Id==item.Authority_Id)>0)
                {
                     getList.Add(item);
                }
            }
            return JsonConvert.SerializeObject(getList);
            #region
            //循环一级目录
            //foreach(var item in query)
            //{

            //    if(list.Count(p=>p.Authority_Id==item.Authority_Id)>0)
            //    {
            //        //存放一级目录标题
            //        strhtml += "<dt><span>" + item.Authority_Name + "</span></dt>";
            //        //根据父级ID查询子集目录
            //        var chilNode = new AuthorityBLL().GetListAuthority().Where(n => n.Authority_Id == item.ParentId);
            //        strhtml += "<dd>";
            //        //循环子集节点并显示
            //        foreach (var childer in chilNode)
            //        {
            //            if(list.Count(p=>p.Authority_Id==childer.Authority_Id)>0)
            //            {
            //                strhtml = "a href='" + childer.Authority_Url + "'>" + childer.Authority_Name + "</a>";

            //            }
            //        }
            //        strhtml += "</dd>";
            //    }
            //}
            //strhtml += "</dl>";
            //return strhtml.ToString();
            #endregion
        }
        #endregion
        #region 下拉框绑定
         //<summary>
         //绑定下拉框
         //</summary>
         //<returns></returns>
        [HttpGet]
        [ActionName("DictionaryBD")]
        public string DictionaryBD(int id) 
        {

            List<SysDictionary> Dictionary = new SysDictionaryBLL().DictionaryList(id); ;
            var HtmlStr = "";
            for (var i = 0; i < Dictionary.Count; i++)
            {
                HtmlStr += "<option value=\"" + Dictionary[i].Dictionary_Number + "\">" + Dictionary[i].Dictionary_Name + "</option>";
            }
            return HtmlStr;
        }

        #endregion
        #region  ///字典的类型
        [HttpGet]
        [ActionName("GetSelectType")]
        public dynamic GetSelectType()
        {
            var doopDic = EnumHelperDic.EnumListDic<EnumSelectType>(0, "--请选择字典类型--");
            return JsonConvert.DeserializeObject(doopDic);
        }
        #endregion 

        #region/// 项目状态
        [HttpGet]
        [ActionName("GetSelectState")]
        public dynamic GetSelectState(int id)
        {
            var doopDic = EnumHelperDic.GetEnumSelectState<EnumSelectState>(id);
            return JsonConvert.DeserializeObject(doopDic);
        }
        /// <summary>
        /// 改变项目状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="projectnum"></param>
        /// <returns></returns>
        [HttpPut]
        [ActionName("PutState")]
        public bool PutState(int  id,string projectnum)
        {
            Basic_Info info = new Basic_Info();
            info.ProjectNumber = projectnum;
            info.Basic_State = id;
            return new Basic_InfoBLL().UpdateState(info);
        }
        #endregion

        #region 立项列表显示(不全)
        [HttpGet]
        [ActionName("GetBasic_Info")]
        public IEnumerable<Basic_Info>  GetBasic_Info(string name) 
        {
            Basic_Info info = new Basic_Info();
            if (name == null || name == "")
            {
                return new Basic_InfoBLL().GetBasicShow(info);
            }
            else
            {
                return new Basic_InfoBLL().GetBasicShow(info).Where(n => n.ProjectNumber.Contains(name));
            }
        }

        #endregion
        // GET api/index/5
        public string Get(int id)
        {
            return "value";
        }
        #region 
        ///// <summary>
        ///// 联系人
        ///// </summary>
        ///// <param name="info"></param>
        ///// <returns></returns>
        //[HttpPost]
        //[ActionName("PostLoan_Info")]
        //// POST api/index
        //public bool PostLoan_Info([FromBody]Loan_Info info)
        //{
        //    SqlParameter[] param ={
        //                             new SqlParameter("@Loan_Contacts",info.Loan_Contacts),
        //                             new SqlParameter("@Loan_ContactsTEL",info.Loan_ContactsTEL),
        //                             new SqlParameter("@Create_People",info.Create_People),
        //                             new SqlParameter("@CreateTime",info.CreateTime),
        //                             new SqlParameter("@Loan_IdCard",info.Loan_IdCard),
        //                             new SqlParameter("@Loan_Name",info.Loan_Name),
        //                             new SqlParameter("@Loan_TEL",info.Loan_TEL),
        //                             new SqlParameter("@ProjectNumber",info.ProjectNumber),
        //                         };
        //    return new Loan_InfoBLL().AddLoan_Info(param);
        //}
        #endregion
        #region 获取项目编号
        [HttpPost]
        [ActionName("PostProjectNumber")]
        public string PostProjectNumber()
        {
            return new ProjectNumberBLL().GetLastNum();
        }
        #endregion
        #region 登记
        [HttpPost]
        [ActionName("PostBasic_Info")]
        // POST api/index
        public bool PostBasic_Info([FromBody]Basic_Info info)
        {
            info.ProjectNumber = PostProjectNumber();
            SqlParameter[] param ={
                                     new SqlParameter("@Surplus_Years",info.Surplus_Years),
                                     new SqlParameter("@Basic_Quota",info.Basic_Quota ),
                                     new SqlParameter("@House_Located",info.House_Located),
                                     new SqlParameter("@House_Purpose",info.House_Purpose),
                                     new SqlParameter("@House_Time",info.House_Time),
                                     new SqlParameter("@Yers_Source",info.Yers_Source),
                                     new SqlParameter("@Presentation_Type",info.Presentation_Type),
                                     new SqlParameter("@Assessment",info.Assessment),
                                     new SqlParameter("@The_Department",info.The_Department),
                                     new SqlParameter("@Survey",info.Survey),
                                     new SqlParameter("@Evaluation",info.Evaluation),
                                     new SqlParameter("@Basic_State",1),
                                     new SqlParameter("@ProjectNumber",info.ProjectNumber),
                                     new SqlParameter("@Issue_Time",info.Issue_Time),
                                     new SqlParameter("@Basic_CreateTime",DateTime.Now),
                                  };
            SqlParameter[] param1  ={
                                     new SqlParameter("@Loan_Contacts",info.Loan_Contacts),
                                     new SqlParameter("@Loan_ContactsTEL",info.Loan_ContactsTEL),
                                     new SqlParameter("@Create_People",info.Create_People),
                                     new SqlParameter("@CreateTime",info.CreateTime),
                                     new SqlParameter("@Loan_IdCard",info.Loan_IdCard),
                                     new SqlParameter("@Loan_Name",info.Loan_Name),
                                     new SqlParameter("@Loan_TEL",info.Loan_TEL),
                                     new SqlParameter("@ProjectNumber",info.ProjectNumber),
                                 };
            var result= new Basic_InfoBLL().AddBasic(param);
            var result1 = new Loan_InfoBLL().AddLoan_Info(param1);
            if(result==true&&result1==true)
            {
                return true;
            }
            else
            {
                return false;
            }
       }
        #endregion
        

        // PUT api/index/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/index/5
        public void Delete(int id)
        {
        }
    }
}
